import 'package:flutter/material.dart';
import 'package:get/get_utils/src/extensions/context_extensions.dart';
import 'package:yysk_app/model/tab_model.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';

class TabWidget extends StatefulWidget {
  final List<TabModel> tabList;
  final ValueChanged<String>? onChanged;
  final bool? sign;
  final String? typeSign;
  final Color? activeColor;
  final Color? tabColor;
  final Color? defalutTabColor;
  final Color? activeBgColor;
  final Color? tabBgColor;
  final Color? currentUndelineColorStart;
  final Color? currentUndelineColorEnd;
  final bool? showTabBgLinearColor; // 为了展示渐变色按钮类的tab
  final String? value;
  final int? current;
  final double isCurrentFontSize;
  final double notCurrentFontSize;
  final double height;
  final bool? currentUndeline;
  final bool? showDefalutTab;
  final double crossAxisSpacing;
  final String? rowMainAxisAlignment;
  final int? undelineHeight;
  final String? fontFamily;
  final int? rightPadding;

  const TabWidget({
    super.key,
    required this.tabList,
    this.onChanged,
    this.sign = false,
    this.typeSign = '',
    this.activeColor = AppColors.fontColorBlack,
    this.tabColor = AppColors.fontColorGrey,
    this.currentUndelineColorStart = AppColors.fontColorBlack,
    this.currentUndelineColorEnd = AppColors.fontColorBlack,
    this.value,
    this.current = 0,
    this.isCurrentFontSize = 18,
    this.notCurrentFontSize = 16,
    this.height = 50,
    this.currentUndeline = false,
    this.crossAxisSpacing = 24,
    this.rowMainAxisAlignment,
    this.fontFamily,
    this.activeBgColor,
    this.tabBgColor,
    this.defalutTabColor,
    this.rightPadding = 0,
    this.undelineHeight = 6,
    this.showDefalutTab = false,
    this.showTabBgLinearColor = false,
  });

  @override
  State<TabWidget> createState() => _TabWidgetState();
}

class _TabWidgetState extends State<TabWidget> {
  String? _current;

  @override
  void initState() {
    super.initState();
    _current =
        widget.value ??
        (widget.tabList.isNotEmpty
            ? widget.tabList[widget.current!].code
            : null);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: context.width,
      padding: EdgeInsets.only(right: widget.rightPadding!.px),
      child: _items(context),
    );
  }

  _items(BuildContext context) {
    List<Widget> items = [];
    for (var i = 0; i < widget.tabList.length; i++) {
      items.add(_item(context, widget.tabList[i], i));
    }
    return Row(
      mainAxisAlignment:
          widget.rowMainAxisAlignment == 'spaceAround'
              ? MainAxisAlignment.spaceAround
              : (widget.rowMainAxisAlignment == 'center'
                  ? MainAxisAlignment.center
                  : MainAxisAlignment.start),
      crossAxisAlignment: CrossAxisAlignment.center,
      children: items,
    );
  }

  Widget _item(BuildContext context, TabModel model, int index) {
    final value = widget.value ?? _current;
    var isCurrent = value == model.code;
    return GestureDetector(
      onTap: () {
        setState(() {
          _current = model.code;
        });
        if (widget.onChanged != null) {
          widget.onChanged!(_current!);
        }
      },
      child: Container(
        decoration: BoxDecoration(),
        child: Row(
          children: [
            index > 0 ? hiSpace(width: widget.crossAxisSpacing) : Container(),
            SizedBox(
              height: widget.height.px,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  model.bySelf == true
                      ? (isCurrent
                          ? model.child
                          : widget.showDefalutTab == true
                          ? model.child
                          : (widget.defalutTabColor != null
                              ? Container(
                                constraints: BoxConstraints(minWidth: 22.px),
                                height: 28.px,
                                decoration: BoxDecoration(
                                  color: widget.defalutTabColor,
                                  borderRadius: BorderRadius.circular(4.px),
                                ),
                                child: Center(
                                  child: Text(
                                    model.label,
                                    style: TextStyle(
                                      color: widget.tabColor,
                                      fontSize: widget.notCurrentFontSize,
                                      fontWeight: FontWeight.normal,
                                      fontFamily: widget.fontFamily,
                                    ),
                                  ),
                                ),
                              )
                              : Container(
                                constraints: BoxConstraints(minWidth: 72.px),
                                height: 28.px,
                                child: Column(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(
                                      model.label,
                                      style: TextStyle(
                                        color: widget.tabColor,
                                        fontSize: widget.notCurrentFontSize,
                                        fontWeight: FontWeight.normal,
                                        fontFamily: widget.fontFamily,
                                      ),
                                    ),
                                    SizedBox(width: 16.px, height: 2.px),
                                  ],
                                ),
                              )))
                      : (widget.activeBgColor != null
                          ? (widget.showTabBgLinearColor == true
                              ? Container(
                                padding: EdgeInsets.fromLTRB(
                                  12.px,
                                  4.px,
                                  12.px,
                                  4.px,
                                ),
                                decoration: BoxDecoration(
                                  gradient: LinearGradient(
                                    begin: Alignment.centerLeft,
                                    end: Alignment.centerRight,
                                    colors:
                                        isCurrent
                                            ? [
                                              AppColors.fontColorStart00,
                                              AppColors.fontColorEnd00,
                                            ]
                                            : [
                                              AppColors.backgroundColorGrey2,
                                              AppColors.backgroundColorGrey2,
                                            ],
                                  ),
                                  borderRadius: BorderRadius.circular(6.px),
                                ),

                                child: Text(
                                  model.label,
                                  style: TextStyle(
                                    color:
                                        isCurrent
                                            ? widget.activeColor
                                            : widget.tabColor,
                                    fontSize:
                                        isCurrent
                                            ? widget.isCurrentFontSize
                                            : widget.notCurrentFontSize,
                                    fontWeight:
                                        isCurrent
                                            ? FontWeight.bold
                                            : FontWeight.normal,
                                    fontFamily: widget.fontFamily,
                                  ),
                                ),
                              )
                              : Container(
                                padding: EdgeInsets.fromLTRB(
                                  12.px,
                                  4.px,
                                  12.px,
                                  4.px,
                                ),
                                decoration: BoxDecoration(
                                  color:
                                      isCurrent
                                          ? widget.activeBgColor
                                          : widget.tabBgColor,
                                  borderRadius: BorderRadius.circular(6.px),
                                ),

                                child: Text(
                                  model.label,
                                  style: TextStyle(
                                    color:
                                        isCurrent
                                            ? widget.activeColor
                                            : widget.tabColor,
                                    fontSize:
                                        isCurrent
                                            ? widget.isCurrentFontSize
                                            : widget.notCurrentFontSize,
                                    fontWeight:
                                        isCurrent
                                            ? FontWeight.bold
                                            : FontWeight.normal,
                                    fontFamily: widget.fontFamily,
                                  ),
                                ),
                              ))
                          : Stack(
                            children: [
                              Text(
                                model.label,
                                style: TextStyle(
                                  color:
                                      isCurrent
                                          ? widget.activeColor
                                          : widget.tabColor,
                                  fontSize:
                                      isCurrent
                                          ? widget.isCurrentFontSize
                                          : widget.notCurrentFontSize,
                                  fontWeight:
                                      isCurrent
                                          ? FontWeight.bold
                                          : FontWeight.normal,
                                  fontFamily: widget.fontFamily,
                                ),
                              ),
                              widget.currentUndeline == true && isCurrent
                                  ? Positioned(
                                    left: 0,
                                    right: 0,
                                    bottom: 0,
                                    child: Container(
                                      height: widget.undelineHeight!.px,
                                      decoration: BoxDecoration(
                                        gradient: LinearGradient(
                                          colors: [
                                            widget.currentUndelineColorStart!,
                                            widget.currentUndelineColorEnd!,
                                          ], // 定义渐变颜色
                                          begin: Alignment.centerLeft,
                                          end: Alignment.centerRight,
                                        ),
                                      ),
                                    ),
                                  )
                                  : SizedBox(),
                            ],
                          )),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
